{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# 实习五 温湿特征量和部分稳定度指数的计算\n",
    "## 1. 位温和相当位温（起始高度按1000hPa来进行计算）\n",
    "## 2. （2）绘制从7月17日00时-7月22日18时，沿35°N， 135°E的位温，相当位温时间-高度剖面图，并分析是否存在不稳定区域。\n",
    "## 3. 尝试计算位势稳定度指数Eσ和K指数并根据第六章所学内容进行分析。计算公式如下\n",
    "### 2021/11/27"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "########需要用到的模块\n",
    "import os\n",
    "import numpy as np  # 调用numpy\n",
    "import pandas as pd\n",
    "import xarray as xr\n",
    "from math import *\n",
    "import metpy.calc as mpcalc\n",
    "from metpy.units import units\n",
    "import metpy.constants as constants\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.ticker as mticker\n",
    "import cartopy.crs as ccrs\n",
    "import cartopy.feature as cfeature\n",
    "from cartopy.io.shapereader import Reader\n",
    "from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER\n",
    "from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "plt.rcParams['font.sans-serif'] = ['SimHei']  ###防止无法显示中文并设置黑体\n",
    "plt.rcParams['axes.unicode_minus'] = False  ###用来正常显示负号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "##时间处理,加个八小时\n",
    "def shijianchuli(img_name):\n",
    "    bigmonth = ['01', '03', '04', '05', '07', '08', '10', '12']\n",
    "    img_name_year = img_name[:4]\n",
    "    img_name_month = img_name[4:6]\n",
    "    img_name_day = img_name[6:8]\n",
    "    img_name_hour = str(int(img_name[8:10]) + 108)  ###转换成北京时间，并且为了小时显示00这样的格式，加了108，后面再截取\n",
    "\n",
    "    ####对日期的处理，有些加了八小时变成北京时间之后，日期会发生改变，下面就是对日期在闰年、非闰年，大小月等情况时的处理\n",
    "    if int(img_name_hour) > 124:\n",
    "        img_name_day = str(int(img_name_day) + 101)\n",
    "        img_name_day = img_name_day[1:3]\n",
    "        img_name_hour = str(int(img_name_hour) - 24)\n",
    "        img_name_hour = img_name_hour[1:3]\n",
    "        if int(img_name_year) % 4 == 0 and int(img_name_year) % 100 != 0:  ####闰年的判定\n",
    "            if img_name_month == '02':\n",
    "                if int(img_name_day) > 29:\n",
    "                    img_name_day = '01'\n",
    "            if img_name_month in bigmonth:\n",
    "                if int(img_name_day) > 31:\n",
    "                    img_name_month = str(int(img_name_month) + 1)\n",
    "                    img_name_day = '01'\n",
    "            else:\n",
    "                if int(img_name_day) > 30:\n",
    "                    img_name_month = str(int(img_name_month) + 1)\n",
    "                    img_name_day = '01'\n",
    "        else:\n",
    "            if img_name_month == '02':\n",
    "                if int(img_name_day) > 28:\n",
    "                    img_name_day = '01'\n",
    "            if img_name_month in bigmonth:\n",
    "                if int(img_name_day) > 31:\n",
    "                    img_name_month = str(int(img_name_month) + 1)\n",
    "                    img_name_day = '01'\n",
    "            else:\n",
    "                if int(img_name_day) > 30:\n",
    "                    img_name_month = str(int(img_name_month) + 1)\n",
    "                    img_name_day = '01'\n",
    "    else:\n",
    "        img_name_hour = img_name_hour[1:3]\n",
    "    daytime = img_name_year + '_' + img_name_month + '_' + img_name_day + '_' + img_name_hour\n",
    "    return daytime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "all = xr.open_dataset(r'D:\\python\\tianzhen\\shixi3_4\\ds_hgt_t_uv.nc')\n",
    "rh_all = xr.open_dataset(r'D:\\python\\tianzhen\\shixi3_4\\ds_rh.nc')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "all"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "rh_all"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "hgt = all['hgt'][:, :, :, :]\n",
    "Temp = all['t'][:, :, :, :]\n",
    "rh = rh_all['rh'][:, :, :, :]\n",
    "lon = all['lon'][:]\n",
    "lat = all['lat'][:]\n",
    "time = all['time'][:]\n",
    "level = rh_all['level'][:]\n",
    "level1 = all['level'][:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "hgt=hgt*units.dagpm\n",
    "Temp=Temp*units.degC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "time.data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "P=level1*units.hPa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "weiwen = np.zeros((len(level1), len(time), len(lat), len(lon)))\n",
    "for h_i in range(len(level1)):\n",
    "    weiwen= mpcalc.potential_temperature(P,Temp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "weiwen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
